// pages/home/home.js
const httpUtils = require('../../../utils/httpUtils')
const ui = require('../../../utils/ui')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 0,
    autoplay: false,
    duration: 500,
    interval: 5000,
    swiperList: [],
    memberNum: 0,
    earning: 0,
    topUp: 0,
    border: {
      color: '#f6f6f6',
    },
    showMultiTextAndTitle: false,
    confirmBtn: { content: '知道了', variant: 'base' },
    notice: {
      content: ''
    }
  },
  //关闭公告弹窗
  closeDialog: function () {
    let id = this.data.notice.id;
    let obj = {
      method: 'GET',
      showLoading: false,
      url: 'notice/readNotice?id=' + id
    }
    httpUtils.request(obj).then(res => {
      if (res.data.retCode == 0) {
        this.setData({
          notice: { content: '' },
          showMultiTextAndTitle: false
        })
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
  },
  //查询系统公告
  getNoticeInfo: function () {
    let obj = {
      method: 'GET',
      showLoading: false,
      url: 'notice/getMyNotice'
    }
    httpUtils.request(obj).then(res => {
      if (res.data.retCode == 0) {
        if(null != res.data.data){
          this.setData({
            notice: res.data.data,
            showMultiTextAndTitle: true
          })
        }
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
  },
  //点击了充值按钮
  getTopUpPage: function (e) {
    console.log('点击了充值按钮')
    wx.navigateTo({
      url: '/pages/my-pages/top-up-list/index',
    })
  },
  //快捷充值按钮
  shortcutTopUp: function (e) {
    console.log('点击了快捷充值按钮')
    wx.navigateTo({
      url: '/pages/my-pages/top-up-add/index',
    })
  },
  //快捷消费按钮
  shortcutExpense: function (e) {
    console.log('点击了快捷消费按钮')
    wx.navigateTo({
      url: '/pages/my-pages/expense-add/index',
    })
  },
  //点击了消费按钮
  getExpensePage: function (e) {
    console.log('点击了消费按钮')
    wx.navigateTo({
      url: '/pages/my-pages/expense-list/index',
    })
  },
  onTap: function (e) {
    const { index } = e.detail;
    console.log(index);
  },
  onChange: function (e) {
    const { current, source } = e.detail;
    console.log(current, source);
  },
  //查询轮播图集合
  getSwiperList: function () {
    let obj = {
      method: 'GET',
      showLoading: false,
      url: 'swiper/getSwiperList'
    }
    httpUtils.request(obj).then(res => {
      if (res.data.retCode == 0) {
        this.setData({
          swiperList: res.data.data
        })
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
  },

  //查询基础数据统计
  getCountInfoList: function () {
    let obj = {
      method: 'GET',
      showLoading: false,
      url: 'home/getMiniProjectHome'
    }
    httpUtils.request(obj).then(res => {
      if (res.data.retCode == 0) {
        this.setData({
          memberNum: res.data.data.memberNum,
          earning: res.data.data.earning,
          topUp: res.data.data.topUpAmount,
        })
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
  },

  //查询消费账单
  getExpenseBill: function () {
    let that = this;
    var onRefresh = that.data.onRefresh;//false为重新刷新数据，true为分页累加数据
    let obj = {
      method: 'POST',
      showLoading: false,
      url: 'expenseBill/getPage',
      data: {
        // 'nameOrNo': that.data.value,
        'page': that.data.page,
        'size': that.data.pageSize,
        'column': 'billTime',
        'order': 'desc'
      }
    }
    httpUtils.request(obj).then(res => {
      if (res.data.retCode == 0) {
        var datas = res.data.data.records;
        if (datas != null && datas.length > 0) {
          if (onRefresh) {//false为重新刷新数据，true为分页累加数据
            that.setData({
              expenseList: that.data.expenseList.concat(datas),
              page: res.data.data.current,
              total: res.data.data.pages
            })
          } else {
            that.setData({
              expenseList: datas,
              page: res.data.data.current,
              total: res.data.data.pages
            })
          }
        } else {
          ui.showToast('没有更多数据了')
        }
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getNoticeInfo();
    this.getSwiperList();
    this.getCountInfoList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})